<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

defined('ANS') or die();

echo $Html->dinamicCssLink('www-common|js/swfupload/swfupload.css');
echo "\n".$Html->jsLink('www-common|js/swfupload/swfupload.js');
echo "\n".$Html->jsLink('www-common|js/swfupload/swfupload.cookies.js');
echo "\n".$Html->jsLink('www-common|js/swfupload/jquery.swfupload.js');

$settings = $Config->config['tables'][$Vars->var['connection']][$Vars->var['table']][$Vars->var['field']]->settings[$Vars->var['field']];
$valid = array();

if ($settings['valid_extensions']) {
	foreach ($settings['valid_extensions'] as $each) {
		$valid[] = '*.'.$each;
	}
} else {
	$valid[] = '*';
}
?>

<script type="text/javascript">

$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "<?php echo path().get(array('connection' => $Vars->var['connection'], 'table' => $Vars->var['table'], 'field' => $Vars->var['field'], 'id' => $Vars->var['id'], 'phpcan_action[large-file-upload]' => 1)); ?>",
		file_post_name: 'uploadfile',
		file_size_limit : "<?php echo $settings['max_size']; ?> MB",
		file_types : "<?php echo implode(';', $valid); ?>",
		file_types_description : "<?php __e('Valid files'); ?>",
		file_upload_limit : 1,
		flash_url : "<?php echo $Config->config['paths']['www-common']; ?>js/swfupload/swfupload.swf",
		button_image_url : '<?php echo $Config->config['paths']['www-common']; ?>js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})

	.bind('fileQueued', function(event, file) {
		var listitem = '<li id="'+file.id+'" >'+
			'File: <em>'+file.name+'</em> ('+Math.round(file.size / 1024 / 1024)+' Mb) <span class="progressvalue" ></span>'+
			'<div class="progressbar"><div class="progress"></div></div>'+
			'<p class="status" >Pendente</p>'+
			'<span class="cancel">&nbsp;</span>'+
			'</li>';

		$('#log').append(listitem);

		$('li#'+file.id+' .cancel').bind('click', function() {
			var swfu = $.swfupload.getInstance('#swfupload-control');

			swfu.cancelUpload(file.id);

			$('li#'+file.id).slideUp('fast');
		});

		// start the upload since it's queued

		$(this).swfupload('startUpload');
	})

	.bind('fileQueueError', function(event, file, errorCode, message) {
		alert('O tamaño do arquivo '+file.name+' supera o límite permitido');
	})

	.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued) {
		$('#queuestatus').text('Arquivos escollidos: '+numFilesSelected+' / Arquivos procesados: '+numFilesQueued);
	})

	.bind('uploadStart', function(event, file) {
		$('#log li#'+file.id).find('p.status').text('Subindo...');
		$('#log li#'+file.id).find('span.progressvalue').text('0%');
		$('#log li#'+file.id).find('span.cancel').hide();
	})

	.bind('uploadProgress', function(event, file, bytesLoaded) {
		//Show Progress
		var percentage = Math.round((bytesLoaded/file.size)*100);

		$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
		$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
	})

	.bind('uploadSuccess', function(event, file, serverData) {
		var item = $('#log li#'+file.id);

		item.find('div.progress').css('width', '100%');
		item.find('span.progressvalue').text('100%');

		item.addClass('success').find('p.status').html(serverData);
	})

	.bind('uploadComplete', function(event, file){
		// upload has completed, try the next one in the queue

		$(this).swfupload('startUpload');
	})
});

</script>

<div class="title col10 t2 b4">
	<h2><?php __e('Large files upload'); ?></h2>
	<p><?php __e('Upload file system with transfer information.'); ?></p>
</div>

<div class="col6">
	<div id="swfupload-control">
		<p><?php __e('You can upload files with a maximun size of %s MB', $settings['max_size']); ?></p>

		<input type="button" id="button" />

		<p id="queuestatus"></p>

		<ol id="log"></ol>
	</div>
</div>
